package com.cms.service;

import java.util.ArrayList;
import java.util.List;

import com.cms.model.AppPointTerraceInfo;
import com.cms.model.AppVersionInfo;
import com.cms.model.AppversionTerraceInfo;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class AppVersionInfoServiceImpl extends AbstractDateService implements
		AppVersionInfoService {
	
	@Override
	public boolean deleteAppVersionInfo(AppVersionInfo model) {
		String[] sql = {"delete from t_appversioninfo where id='"+model.getId()+"'",
						"delete from t_appversionterrace where appversionId='"+model.getId()+"'"};
		logger.info("[SQL] [method:deleteAppVersionInfo] ["+sql+"]");
		return ConnectionManager.getInstance().executeBatch(base,sql);
	}
	
	@Override
	public boolean deleteAppVersionInfo1(AppVersionInfo model) {
		String[] sql = {"update t_appversioninfo set isdel='1' where id='"+model.getId()+"'"};
		logger.info("[SQL] [method:deleteAppVersionInfo1] ["+sql+"]");
		return ConnectionManager.getInstance().executeBatch(base,sql);
	}

	@Override
	public List<AppVersionInfo> getAppVersionInfoByAppNumber(String appNumber) {
		String sql = "select * from t_appversioninfo where appnumber='"+appNumber+"' and isdel='0' order by createdate desc";
		logger.info("[SQL] [method:getAppVersionInfoByAppNumber] ["+sql+"]");
		ModelGenerator<AppVersionInfo> model = new ModelGenerator<AppVersionInfo>(AppVersionInfo.class);
		return model.generateList(base,sql.toString());
	}
	

	@Override
	public List<AppVersionInfo> getAppVersionInfoByAppVersionNumber(String appVersionNumber){
		String sql = "select * from t_appversioninfo where appversionnumber='"+appVersionNumber+"' order by createdate desc";
		logger.info("[SQL] [method:getAppVersionInfoByAppVersionNumber] ["+sql+"]");
		ModelGenerator<AppVersionInfo> model = new ModelGenerator<AppVersionInfo>(AppVersionInfo.class);
		return model.generateList(base,sql.toString());
	}
	@Override
	public AppVersionInfo getAppVersionInfoById(int id) {
		String sql = "select * from t_appversioninfo where id='"+id+"' order by createdate desc";
		logger.info("[SQL] [method:getAppVersionInfoById] ["+sql+"]");
		ModelGenerator<AppVersionInfo> model = new ModelGenerator<AppVersionInfo>(AppVersionInfo.class);
		return model.generate(base,sql.toString());
	}

	@Override
	public List<AppVersionInfo> getAppVersionInfos() {
		String sql = "select * from t_appversioninfo where isdel='0' order by createdate desc";
		logger.info("[SQL] [method:getAppVersionInfos] ["+sql+"]");
		ModelGenerator<AppVersionInfo> model = new ModelGenerator<AppVersionInfo>(AppVersionInfo.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public boolean saveAppVersionInfo(AppVersionInfo model) {
		model.setId(getTableId("t_appversioninfo"));
		String sql = "insert into t_appversioninfo (id,appversionnumber,appnumber,appname,cpid,categoryparentid,categoryid"+
		",apppackage,appstatus,appversion,appmd5,appiconname,reiconname,appiconsrc,appfilename"+
		",refilename,appfilesrc,appentry,createuser,createdate,appremark,provinceids,apppointids,recommend) " +
		"values ('"+model.getId()+"','"+model.getAppversionnumber()+"','"+model.getAppnumber()+"','"+model.getAppname()+"','"+model.getCpid()+"','"+model.getCategoryparentid()+"','"+model.getCategoryid()+
		"','"+model.getApppackage()+"','"+model.getAppstatus()+"','"+model.getAppversion()+"','"+model.getAppmd5()+"','"+model.getAppiconname()+
		"','"+model.getReiconname()+"','"+model.getAppiconsrc()+"','"+model.getAppfilename()+"','"+model.getRefilename()+"','"+model.getAppfilesrc()+
		"','"+model.getAppentry()+"','"+model.getCreateuser()+"','"+model.getCreatedate()+"','"+model.getAppremark()+"','"+model.getProvinceids()+
		"','"+model.getApppointids()+"','"+model.getRecommend()+"')";
		logger.info("[SQL] [method:saveAppVersionInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean saveAppVersionInfoStatus(AppVersionInfo model) {
		String sql = "update t_appversioninfo set appstatus='" + model.getAppstatus()+ "' " +
		" where id='" + model.getId() + "'";
		logger.info("[SQL] [method:saveAppVersionInfoStatus] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateAppVersionInfo(AppVersionInfo model) {
		String sql = "update t_appversioninfo set appname='" + model.getAppname() + "'," +
		"apppackage='" + model.getApppackage() + "', " +
		"appversion='" + model.getAppversion() + "' " +
		",appmd5='" + model.getAppmd5()+ "' " +
		",appiconname='" + model.getAppiconname() + "' " +
		",reiconname='" + model.getReiconname() + "' " +
		",appiconsrc='" + model.getAppiconsrc() + "' " +
		",appfilename='" + model.getAppfilename() + "' " +
		",refilename='" + model.getRefilename() + "' " +
		",appfilesrc='" + model.getAppfilesrc() + "' " +
		",appentry='" + model.getAppentry() + "' " +
		",appremark='" + model.getAppremark() + "' " +
		",provinceids='"+model.getProvinceids()+"'"+
		",apppointids='"+model.getApppointids()+"'"+
		",appversionnumber='"+model.getAppversionnumber()+"'"+
		",recommend='"+model.getRecommend()+"'"+
		" where id='" + model.getId() + "'";
		logger.info("[SQL] [method:updateAppVersionInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}
	
	@Override
	public boolean updateAppVersionFile(AppVersionInfo model) {
		String sql = "update t_appversioninfo set appfilename='" + model.getAppfilename() + "' " +
				",refilename='" + model.getRefilename() + "' " +
				",appfilesrc='" + model.getAppfilesrc() + "' " +
				" where appversionnumber='" + model.getAppversionnumber() + "'";
				logger.info("[SQL] [method:updateAppVersionFile] ["+sql+"]");
				return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateAppVersionRecommend(AppVersionInfo model){
		String sql = "update t_appversioninfo set recommend='"+model.getRecommend()+"' where id='"+model.getId()+"'";
		return ConnectionManager.getInstance().execute(base,sql);
	}
	@Override
	public List<AppversionTerraceInfo> getByAppversionId(Integer appversionId) {
		String sql = "select  b.id as terraceId ,b.operatorsName, a.appversionId, a.remark as glremark ,a.id, a.relevanceid from  "
				+"t_terrace b "
				+"  inner join (select * from t_appversionterrace  where appversionId="+ appversionId +" )  a"
				+" on a.terraceId=b.id" ;
			logger.info("sql: " + sql);
			ModelGenerator<AppversionTerraceInfo> dbcpmg = new ModelGenerator<AppversionTerraceInfo>(AppversionTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}

	@Override
	public boolean updateAppversionTerraceInfo(AppVersionInfo appVersion,
			List<AppversionTerraceInfo> appversionTerraceInfos,List<AppPointTerraceInfo> apppointTerraceInfos) {
		if(appversionTerraceInfos==null){
			appversionTerraceInfos =new ArrayList<AppversionTerraceInfo>();
		}
		
		if(apppointTerraceInfos == null){
			apppointTerraceInfos = new ArrayList<AppPointTerraceInfo>();
		}
		//更新应用版本信息
		this.updateAppVersionInfo(appVersion);
		//更新应用版本平台的信息
		String sql = "delete from t_appversionterrace where appversionId=" + appVersion.getId();
		ConnectionManager.getInstance().execute(base, sql);
		//添加应用版本平台关联信息
		for(AppversionTerraceInfo appversionTerraceInfo : appversionTerraceInfos){
			if (appversionTerraceInfo.getAppversionId() <= 0)
				appversionTerraceInfo.setAppversionId(appVersion.getId());
			this.insertAppversionTerraceInfo(appversionTerraceInfo);
		}
		String sql1 = "delete from t_apppointterrace where appversionId=" + appVersion.getId();
		ConnectionManager.getInstance().execute(base, sql1);
		//添加应用计费与平台的关联信息
		for(AppPointTerraceInfo apt: apppointTerraceInfos){
			if(apt.getAppVersionId() <= 0)
				apt.setAppVersionId(appVersion.getId());
			this.insertApppointTerraceInfo(apt);
		}
			
		return true;
	}
	
	
	@Override
	public boolean insertAppversionTerraceInfo(AppVersionInfo appVersion,
			List<AppversionTerraceInfo> AppversionTerraceInfos,List<AppPointTerraceInfo> apppointTerraceInfos) {
		if(AppversionTerraceInfos==null){
			AppversionTerraceInfos =new ArrayList<AppversionTerraceInfo>();
		}
		//添加应用版本信息
		this.saveAppVersionInfo(appVersion);
		//添加应用版本平台关联信息
		for(AppversionTerraceInfo av: AppversionTerraceInfos){
			av.setAppversionId(appVersion.getId());
			this.insertAppversionTerraceInfo(av);
		}
		//添加应用计费与平台的关联信息
		for(AppPointTerraceInfo apt: apppointTerraceInfos){
			if(apt.getAppVersionId() <= 0)
				apt.setAppVersionId(appVersion.getId());
			this.insertApppointTerraceInfo(apt);			
		}
		return true;
	}
	
	public boolean insertAppversionTerraceInfo(AppversionTerraceInfo appversionTerraceInfo){		
		appversionTerraceInfo.setId(getTableId("t_appversionterrace"));
		String sql = 
			"insert into t_appversionterrace(id,appversionId,relevanceid,terraceId,remark) " +
			"values(" + String.valueOf(appversionTerraceInfo.getId()) + ","+String .valueOf(appversionTerraceInfo.getAppversionId()) +",'" +appversionTerraceInfo.getRelevanceid() + "',"
					+String.valueOf(appversionTerraceInfo.getTerraceId())+ ",'" +appversionTerraceInfo.getGlremark()+"')";
		System.out.println("insertAppversionTerraceInfo sql=========="+sql);
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public List<AppversionTerraceInfo> queryAppversionTerrace() {
		String sql = "select  b.id as terraceId ,b.operatorsName,a.appversionId, a.remark as glremark ,a.id, a.relevanceid from "
				+"(select * from t_terrace ) b"
				+" left join t_appversionterrace a"
				+" on a.terraceId=b.id" ;
			logger.debug("sql: " + sql);
			ModelGenerator<AppversionTerraceInfo> dbcpmg = new ModelGenerator<AppversionTerraceInfo>(AppversionTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}

	@Override
	public List<AppversionTerraceInfo> queryAppversionTerraceByAppNumber(String appNumber) {
		String sql = "select  b.id as terraceId ,b.operatorsName,a.appversionId, a.remark as glremark ,a.id, a.relevanceid from "
				+" t_terrace b,t_appversionterrace a"
				+" where a.terraceId=b.id and a.appversionId in (select id from t_appversioninfo where appnumber='"+appNumber+"')" ;
			logger.info("[method:queryAppversionTerraceByAppNumber] [sql]: " + sql);
			ModelGenerator<AppversionTerraceInfo> dbcpmg = new ModelGenerator<AppversionTerraceInfo>(AppversionTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}
	@Override
	public List<AppversionTerraceInfo> queryApvtByAppnumberAndTerraceId(String appnumber,String terraceId) {
		String sql = "select  b.id as terraceId ,b.operatorsName,a.appversionId, a.remark as glremark ,a.id, a.relevanceid from "
				+" t_terrace b,t_appversionterrace a"
				+" where a.terraceId=b.id and a.terraceId='"+terraceId+"' and a.appversionId in (select id from t_appversioninfo where appnumber='"+appnumber+"')" ;
		logger.info("[SQL] [method:queryApvtByAppnumberAndTerraceId] ["+sql+"]");
		ModelGenerator<AppversionTerraceInfo> dbcpmg = new ModelGenerator<AppversionTerraceInfo>(AppversionTerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}
	public boolean insertApppointTerraceInfo(AppPointTerraceInfo apppointTerraceInfo){		
		apppointTerraceInfo.setPtId(getTableId("t_apppointterrace"));
		String sql = 
			"insert into t_apppointterrace(id,apppointId,appversionId,pointcode,terraceId,remark) " +
			"values(" + String.valueOf(apppointTerraceInfo.getPtId()) + ","+ String .valueOf(apppointTerraceInfo.getApppointId()) + "," + String.valueOf(apppointTerraceInfo.getAppVersionId()) + ",'" 
					+apppointTerraceInfo.getPointCode() + "'," + String.valueOf(apppointTerraceInfo.getTerraceId()) + ",'" +apppointTerraceInfo.getGlremark()+"')";
		System.out.println("insertApppointTerraceInfo sql=========="+sql);
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public List<AppPointTerraceInfo> queryAppPointTerraceInfo() {
		String sql = "select  b.id as pointId ,b.pointofbillingname,a.appversionId,a.apppointId ,a.id, a.pointcode,c.id as terraceId, c.id ,c.operatorsName from   "
				+" t_apppointofbilling  b, t_apppointterrace a ,t_terrace c where  "
				+"a.apppointId=b.id and c.id=a.terraceId order by  a.terraceId,a.apppointId ";
			logger.info("[method:queryAppPointTerraceInfo] [sql:] " + sql);
			ModelGenerator<AppPointTerraceInfo> dbcpmg = new ModelGenerator<AppPointTerraceInfo>(AppPointTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}
	@Override
	public List<AppPointTerraceInfo> queryAppPointTerraceInfoByAppNumber(String appNumber) {
		String sql = "select  b.id as pointId ,b.pointofbillingname,a.appversionId,a.apppointId ,a.id, b.code as pointcode,c.id as terraceId, c.id ,c.operatorsName,b.fee as fee,b.type as codetype,b.triggerremark as codetriggerremark,b.propremark as codepropremark,b.remark as coderemark from  "
				+" t_apppointofbilling  b, t_apppointterrace a ,t_terrace c where  "
				+"a.apppointId=b.id and c.id=a.terraceId and a.appversionId in (select id from t_appversioninfo where appnumber='"+appNumber+"') order by  a.terraceId,a.apppointId ";
			logger.info("[method:queryAppPointTerraceInfoByAppNumber] [sql:] " + sql);
			ModelGenerator<AppPointTerraceInfo> dbcpmg = new ModelGenerator<AppPointTerraceInfo>(AppPointTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}
	@Override
	public List<AppPointTerraceInfo> queryAppPointTerraceInfoByAppVersionId(int appversionId) {
		String sql = "select  b.id as pointId ,b.pointofbillingname,a.appversionId,a.apppointId ,a.id, a.pointcode,c.id as terraceId, c.id ,c.operatorsName,b.fee as fee,b.type as codetype,b.triggerremark as codetriggerremark,b.propremark as codepropremark,b.remark as coderemark from  "
				+" t_apppointofbilling  b, t_apppointterrace a ,t_terrace c where  "
				+"a.apppointId=b.id and c.id=a.terraceId and a.appversionId="+appversionId;
			logger.info("[method:queryAppPointTerraceInfoByAppVersionId] [sql:] " + sql);
			ModelGenerator<AppPointTerraceInfo> dbcpmg = new ModelGenerator<AppPointTerraceInfo>(AppPointTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}
}
